package lumis.content.core;

import java.io.Serializable;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import lumis.portal.PortalException;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.stability.StableMinor;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * Represents an association from a {@link ContentVersion} to a {@link Content}.
 * <p>
 * This object also describes the type of association between the content
 * version and the content.
 * </p>
 *
 * @stereotype entity
 * @navassoc * "associated to" 1 Content
 * 
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 * @since 4.2.0
 */
@StableMinor(version = "6.0", sinceVersion = "4.2")
@Entity
@Table(name="lum_ContentAssociation")
@GenericGenerator(name="portal-guid", strategy="lumis.portal.dao.hibernate.GuidGenerator")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@SuppressWarnings("serial")
public class ContentAssociation implements Serializable
{
	public static final String TYPE_STANDARD = "standard";
	public static final String TYPE_CATEGORIZATION_TERM = "categorizationTerm";
	
	@Id @GeneratedValue(generator="portal-guid")
	private String id;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="contentVersionId")
	private ContentVersion contentVersion;
	
	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="associatedContentId",nullable=false)
	private Content associatedContent;
	
	private String type = TYPE_STANDARD;
	
	ContentAssociation()
	{
	}
	
	ContentAssociation(ContentVersion contentVersion)
	{
		this.contentVersion = contentVersion;
	}

	public ContentVersion getContentVersion()
	{
		return contentVersion;
	}

	public Content getAssociatedContent()
	{
		return associatedContent;
	}

	public void setAssociatedContent(Content associatedContent)
	{
		this.associatedContent = associatedContent;
	}

	public String getType()
	{
		return type;
	}

	public void setType(String type)
	{
		this.type = type;
	}

	public String getId()
	{
		return id;
	}
	
	/**
	 * Deletes this content association.
	 * @since 4.2.0
	 */
	public void delete() throws PortalException
	{
		getContentVersion().removeContentAssociation(this);
		ManagerFactory.getEntityManager().remove(this);
	}
}
